package jdbc;

import java.sql.*;
public class day03 {
    public static void main(String[] args) {
        Connection conn = null;
        Statement stmt = null;
        ResultSet rs = null;
        String driver = "com.mysql.cj.jdbc.Driver";

        String url = "jdbc:mysql://localhost:3306/test1?useSSL=false";
        String username = "root";
        String password = "314159";

        try{
            Class.forName(driver);
            conn = DriverManager.getConnection(url, username, password);
            stmt = conn.createStatement();
            String sql = "select empno, ename, job, sal from emp";
            //专门执行查询语句
            rs = stmt.executeQuery(sql);
            //JDBC中下标从1开始
            while(rs.next()){
                //下标表示列
                //也支持值状，如下：名称是查询结果的名称
                //String empno = rs.getString(empno);
                String empno = rs.getString(1);
                String ename = rs.getString(2);
                String job = rs.getString(3);
                String sal = rs.getString(4);
                System.out.println(empno+","+ename+","+job+","+sal);
            }

        } catch (Exception e){
            e.printStackTrace();
        }   finally{
            if(rs!=null){
                try{
                    rs.close();
                } catch(Exception e){
                    e.printStackTrace();
                }
            }
            if(stmt!=null){
                try{
                    stmt.close();
                } catch(Exception e){
                    e.printStackTrace();
                }
            }
            if(conn!=null){
                try{
                    conn.close();
                } catch(Exception e){
                    e.printStackTrace();
                }
            }
        }
    }
}
